﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

using Inventory.Database;
using Inventory.Database.Models;
using Inventory.Domain.Models;
using System.Configuration;
using System.Web;
using ImportRottenTomatoes;

namespace Console
{
    class Program
    {
        static void Main(string[] args)
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<InventoryContext, Inventory.Database.Migrations.Configuration>());
            CreateDatabase();

            //using (var context = new InventoryContext())
            //{
            //    var genre = new MovieGenre {Title = "Test"};
            //    context.MovieGenres.Add(genre);
            //    context.SaveChanges();

            //    var q = from g in context.MovieGenres.OrderBy(g => g.Title) select g;

            //    foreach (var i in q)
            //    {
            //        System.Console.WriteLine(i.Title);
            //    }

                
            //}
            //System.Console.WriteLine("");
            //System.Console.WriteLine("");
            
            using (var repo = new MovieGenreRepository(new UnitOfWork()))
            {
                foreach(var mg in  repo.All)
                {
                    System.Console.WriteLine(mg.Title);
                }
            }

            using (var repo = new MovieRepository(new UnitOfWork()))
            {
                var movie = new Inventory.Domain.Models.Movie();
                movie.Title = "The Hunger Games";
                repo.InsertOrUpdate(movie);
                repo.Save();


            }

            string key = ConfigurationManager.AppSettings["RtKey"].ToString();
            string url = ConfigurationManager.AppSettings["RtUrl"].ToString();

            var import = new ImportMovie(key, url);
            var movie2 = import.GetMovie(770672122);
            if (movie2 != null)
            {
                System.Console.Write("Title: " + movie2.Title + ", Id: " + movie2.RtId + ", year: " + movie2.ReleaseYear);
                System.Console.WriteLine("");
                System.Console.WriteLine("Synopsis:" + movie2.SynopsisLong);
            }

            var movies = import.Search("prometheus", 10);
            foreach (var movie in movies)
            {
                if (movie != null)
                {
                    System.Console.Write("Title: " + movie.Title + ", Id: " + movie.RtId + ", year: " + movie.ReleaseYear);
                    System.Console.WriteLine("");
                }
            }


            //using (var wclient = new WebClient())
            //{
            //    //request a movie

            //    //movie search
            //    url += "/movies.json?apikey=" + key + "&q=Jack&page_limit=2";
            //    //movie by id
            //    //url += "/movies/770672122.json?apikey=" + key;
            //    wclient.Headers.Add("Content-Type", "application/json");
            //    var response = wclient.DownloadString(url);

            //    var ser = new JavaScriptSerializer();
            //    var movieSearch = ser.Deserialize<MovieSearch>(response);
            //    //var movie = ser.Deserialize<ImportRottenTomatoes.Movie>(response);
            //}
 
            
            System.Console.ReadLine();
        }

        private static void CreateDatabase()
        {
            var context = new InventoryContext();
            //default is create database if not exists
            context.Database.Initialize(true);
        }
    }


}
